package com.syt.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.syt.eduservice.Listener.SubjectExcelListener;
import com.syt.eduservice.entity.EduSubject;
import com.syt.eduservice.entity.excel.SubjectData;
import com.syt.eduservice.entity.vo.OneSubject;
import com.syt.eduservice.entity.vo.TwoSubject;
import com.syt.eduservice.mapper.EduSubjectMapper;
import com.syt.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.swing.plaf.ListUI;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author SYT
 * @since 2021-03-04
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {
    //添加课程分类
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService subjectService) {
        try {
            InputStream in = file.getInputStream();
            //调用方法
            EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<OneSubject> getAllSubject() {
        //1.查询所有一级分类 parent_id = 0
        QueryWrapper<EduSubject> wrapperOne =new QueryWrapper<>();
        wrapperOne.eq("parent_id",0);
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);
        //2.查询所欲二级分类 parent_id != 0
        QueryWrapper<EduSubject> wrapperTwo =new QueryWrapper<>();
        wrapperTwo.ne("parent_id",0);
        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapperTwo);
        //创建list数据,用于存储最终的封装数据
        List<OneSubject> finalSubjectList = new ArrayList<>();
        //3.封装一级分类
        for (EduSubject eduSubject : oneSubjectList) {
            OneSubject oneSubject = new OneSubject();
            BeanUtils.copyProperties(eduSubject,oneSubject,OneSubject.class);
            finalSubjectList.add(oneSubject);
            //4.封装二级分类
            List<TwoSubject> twoFinalSubjects = new ArrayList<>();
            for (EduSubject subject : twoSubjectList) {
                //判断二级分类的parentid和一级分类是否一样
                if (subject.getParentId().equals(eduSubject.getId())){
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(subject,twoSubject);
                    twoFinalSubjects.add(twoSubject);
                }
            }
            oneSubject.setChildren(twoFinalSubjects);
        }



        return finalSubjectList;
    }
}
